package com.wenhai.liuyubobobo.imooc.sort;

import java.util.function.Consumer;

/**
 * 排序算法 辅助类
 *
 * @author wenhai
 * @date 2020/9/10
 */
public class SortingHelper {
    private SortingHelper() {

    }


    public static <E extends Comparable<E>> boolean isSorted(E[] arr) {
        if (arr.length < 1) {
            return false;
        }
        for (int i = 1; i < arr.length; i++) {
            if (arr[i - 1].compareTo(arr[i]) > 0) {
                return false;
            }
        }
        return true;
    }

    public static <E extends Comparable<E>> void sortTest(String sortName, Consumer<E[]> sortType, E[] arr) {
        long startTime = System.nanoTime();
        sortType.accept(arr);
        long endTime = System.nanoTime();
        if (!isSorted(arr)) {
            throw new RuntimeException(String.format("[%s]算法不正确", sortName));
        }
        double costTime = (endTime - startTime) / 1000000000.0;
        System.out.println(String.format("%s, n=%d  花费时间:%fs", sortName, arr.length, costTime));
    }
}
